import com.gc.db.DBUtil;
import com.gc.db.Tables;
import com.gc.fm.FmReadWrite;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class hello {
    private JComboBox modelcombox;
    private JCheckBox serviceCheckBox;
    private JCheckBox servletCheckBox;
    private JCheckBox controllerCheckBox;
    private JCheckBox daoCheckBox;
    private JCheckBox entityCheckBox;
    private JCheckBox webXmlCheckBox;
    private JCheckBox springXmlCheckBox;
    private JCheckBox springmvcXmlCheckBox1;
    private JCheckBox mybatisConfigXmlCheckBox;
    private JCheckBox mybatisMapperXmlCheckBox;
    private JPanel mainpanel;
    private JComboBox dbcombox;
    private JTextField usernametextfield;
    private JButton 面对疾风吧Button;
    private JButton 加载数据库Button;
    private JComboBox tbcombox;
    private JPasswordField PasswordField;
    private JTextField urltextfield;
    private JTextField packageTextField;
    private JTextField outputTextField;
    String driver = "";
    DBUtil dbUtil = null;
    Map<String,Object> map = new HashMap<>();
    public hello(){
        modelcombox.addItem("SSM");
        modelcombox.addItem("JSP+Servlet+JDBC");
        //初始化界面数据
        entityCheckBox.setSelected(true);
        daoCheckBox.setSelected(true);
        serviceCheckBox.setSelected(true);
        springXmlCheckBox.setSelected(true);
        controllerCheckBox.setSelected(true);
        springmvcXmlCheckBox1.setSelected(true);
        mybatisConfigXmlCheckBox.setSelected(true);
        mybatisMapperXmlCheckBox.setSelected(true);
        webXmlCheckBox.setSelected(true);
        servletCheckBox.setSelected(false);

        dbcombox.addItem("MySql");
        dbcombox.addItem("Oracle");
        usernametextfield.setText("root");
        PasswordField.setText("admin");
        urltextfield.setText("jdbc:mysql://localhost:3306/test?characterEncoding=utf8");
        driver="com.mysql.jdbc.Driver";

        modelcombox.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                if(e.getStateChange() == ItemEvent.SELECTED) {
                  String val = e.getItem().toString();
                  if(val.equals("JSP+Servlet+JDBC")){
                      entityCheckBox.setSelected(true);
                      daoCheckBox.setSelected(true);
                      serviceCheckBox.setSelected(true);
                      servletCheckBox.setSelected(true);
                      webXmlCheckBox.setSelected(true);

                      springXmlCheckBox.setSelected(false);
                      controllerCheckBox.setSelected(false);
                      springmvcXmlCheckBox1.setSelected(false);
                      mybatisConfigXmlCheckBox.setSelected(false);
                      mybatisMapperXmlCheckBox.setSelected(false);
                  }else if(val.equals("SSM")){
                      entityCheckBox.setSelected(true);
                      daoCheckBox.setSelected(true);
                      serviceCheckBox.setSelected(true);
                      springXmlCheckBox.setSelected(true);
                      controllerCheckBox.setSelected(true);
                      springmvcXmlCheckBox1.setSelected(true);
                      mybatisConfigXmlCheckBox.setSelected(true);
                      mybatisMapperXmlCheckBox.setSelected(true);
                      webXmlCheckBox.setSelected(true);
                      servletCheckBox.setSelected(false);
                  }else{
                      entityCheckBox.setSelected(false);
                      daoCheckBox.setSelected(false);
                      serviceCheckBox.setSelected(false);
                      servletCheckBox.setSelected(false);
                      webXmlCheckBox.setSelected(false);
                      springXmlCheckBox.setSelected(false);
                      controllerCheckBox.setSelected(false);
                      springmvcXmlCheckBox1.setSelected(false);
                      mybatisConfigXmlCheckBox.setSelected(false);
                      mybatisMapperXmlCheckBox.setSelected(false);
                  }
                }
            }
        });

        dbcombox.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                if(e.getStateChange() == ItemEvent.SELECTED) {
                    String val = e.getItem().toString();
                    if(val.equals("MySql")){
                        urltextfield.setText("jdbc:mysql://localhost:3306/dbname?characterEncoding=utf8");
                        usernametextfield.setText("root");
                        PasswordField.setText("admin");
                        driver="com.mysql.jdbc.Driver";

                    }else if(val.equals("Oracle")){
                        urltextfield.setText("jdbc:oracle:thin:@localhost:1521:orcl");
                        usernametextfield.setText("scott");
                        PasswordField.setText("admin");
                        driver="oracle.jdbc.driver.OracleDriver";

                    }
                    tbcombox.removeAllItems();
                }
            }
        });
        面对疾风吧Button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("面对疾风吧");
                if(dbcombox.getSelectedItem().toString().equals("MySql")){
                    map.put("db",1);
                }else{
                    map.put("db",2);
                }
                String tbname = tbcombox.getSelectedItem().toString();
                String basepath = outputTextField.getText()+"/"+packageTextField.getText().replaceAll("\\.","/")+"/";
                if(tbname!=null && !tbname.equals("")){
                    try {
                        Tables table = dbUtil.getColumInfo(tbname);
                        System.out.println(table.getName());
                        map.put("dir",packageTextField.getText());
                        map.put("table",table);
                        map.put("driver",driver);
                        map.put("url",urltextfield.getText());
                        map.put("username",usernametextfield.getText());
                        map.put("pwd",PasswordField.getText());
                        String model = modelcombox.getSelectedItem().toString();
                        System.out.println("选择的类型是："+model);
                        if(model.equals("SSM")){
                            FmReadWrite.createDir(basepath,2);
                            if(entityCheckBox.isSelected()){
                                FmReadWrite.generate(map,"ssm_mvc/entity.ftl",basepath+"/entity/"+table.getName()+".java");
                            }
                            if(daoCheckBox.isSelected()){
                                FmReadWrite.generate(map,"ssm_mvc/mapper.ftl",basepath+"/mapper/"+table.getName()+"Mapper.java");
                            }
                            if(serviceCheckBox.isSelected()){
                                FmReadWrite.generate(map,"ssm_mvc/service.ftl",basepath+"/service/"+table.getName()+"Service.java");
                                FmReadWrite.generate(map,"ssm_mvc/serviceImpl.ftl",basepath+"/service/impl/"+table.getName()+"ServiceImpl.java");
                            }
                            if(controllerCheckBox.isSelected()){
                                FmReadWrite.generate(map,"ssm_mvc/controller.ftl",basepath+"controller/"+table.getName()+"Controller.java");
                            }
                            if(mybatisMapperXmlCheckBox.isSelected()){
                                FmReadWrite.generate(map,"configurationfile/mybatismapperxml.ftl",basepath+table.getName()+"Mapper.xml");
                            }
                        }else{
                            FmReadWrite.createDir(basepath,1);
                            if(entityCheckBox.isSelected()){
                                FmReadWrite.generate(map,"servlet_jdbc_mvc/entity.ftl",basepath+"/entity/"+table.getName()+".java");
                            }
                            if(daoCheckBox.isSelected()){
                                FmReadWrite.generate(map,"servlet_jdbc_mvc/basedao.ftl",basepath+"/util/BaseDao.java");
                                FmReadWrite.generate(map,"servlet_jdbc_mvc/jdbc.ftl",outputTextField.getText()+"/jdbc.properties");
                                FmReadWrite.generate(map,"servlet_jdbc_mvc/dao.ftl",basepath+"/dao/"+table.getName()+"Dao.java");
                                FmReadWrite.generate(map,"servlet_jdbc_mvc/daoImpl.ftl",basepath+"/dao/impl/"+table.getName()+"DaoImpl.java");
                            }
                            if(serviceCheckBox.isSelected()){
                                FmReadWrite.generate(map,"servlet_jdbc_mvc/service.ftl",basepath+"/service/"+table.getName()+"Service.java");
                                FmReadWrite.generate(map,"servlet_jdbc_mvc/serviceImpl.ftl",basepath+"/service/impl/"+table.getName()+"ServiceImpl.java");
                            }
                            if(servletCheckBox.isSelected()){
                                FmReadWrite.generate(map,"servlet_jdbc_mvc/servlet.ftl",basepath+"servlet/"+table.getName()+"Servlet.java");
                            }
                        }

                    } catch (Exception e1) {
                        e1.printStackTrace();
                        JOptionPane.showMessageDialog(mainpanel, "代码生成失败，请检查日志！", " 标题",JOptionPane.WARNING_MESSAGE);
                        return;
                    }
                }
                try {
                    if(webXmlCheckBox.isSelected()){
                        map.put("hasspring",false);
                        map.put("hasspringmvc",false);
                        if(springXmlCheckBox.isSelected()){
                            map.put("hasspring",true);
                        }
                        if(springmvcXmlCheckBox1.isSelected()){
                            map.put("hasspringmvc",true);
                        }
                        FmReadWrite.generate(map,"configurationfile/webxml.ftl",outputTextField.getText()+"web.xml");
                    }
                    if(mybatisConfigXmlCheckBox.isSelected()){
                        map.put("hasspring",false);
                        if(springXmlCheckBox.isSelected()){
                            map.put("hasspring",true);
                        }
                        FmReadWrite.generate(map,"configurationfile/mybatisconfigxml.ftl",outputTextField.getText()+ "mybatis-config.xml");
                    }
                    if(springXmlCheckBox.isSelected()){
                        map.put("hasmybatis",false);
                        if(mybatisMapperXmlCheckBox.isSelected()||mybatisConfigXmlCheckBox.isSelected()){
                            map.put("hasmybatis",true);
                        }
                        FmReadWrite.generate(map,"configurationfile/springxml.ftl",outputTextField.getText()+"spring.xml");
                    }
                    if(springmvcXmlCheckBox1.isSelected()){
                        FmReadWrite.generate(map,"configurationfile/springmvcxml.ftl",outputTextField.getText()+"springmvc.xml");
                    }
                }catch (Exception e1){
                    e1.printStackTrace();
                    JOptionPane.showMessageDialog(mainpanel, "代码生成失败，请检查日志！", " 标题",JOptionPane.WARNING_MESSAGE);
                    return;
                }
                JOptionPane.showMessageDialog(mainpanel, "代码生成成功！", " 标题",JOptionPane.WARNING_MESSAGE);
            }
        });
        加载数据库Button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                tbcombox.removeAllItems();
                String url = urltextfield.getText();
                String username = usernametextfield.getText();
                String pwd = PasswordField.getText();
                dbUtil = new DBUtil(url,username,pwd,driver);
                List<String> list = null;
                try {
                    list = dbUtil.getTableInfo();
                    for(String t: list){
                        tbcombox.addItem(t);
                    }
                } catch (Exception e1) {
                    e1.printStackTrace();
                    JOptionPane.showMessageDialog(mainpanel, "数据库加载失败，请重新操作！", " 标题",JOptionPane.WARNING_MESSAGE);
                }
            }
        });
    }
    public static void main(String[] args) {
        JFrame frame = new JFrame("HelloWorld");
        frame.setContentPane(new hello().mainpanel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }



}
